import {Component, OnInit, ViewChild} from '@angular/core';
import {FormBuilder, FormGroup} from "@angular/forms";
import {Purpose} from "../../shared/model/purpose";
import {UdgTableComponent} from "../../shared/component/udg-table/udg-table.component";
import {FileUploadComponent} from "../../shared/component/file-upload/file-upload.component";
import {ActivatedRoute, Router} from "@angular/router";
import {DriverTableComponent} from "../../shared/component/driver-table/driver-table.component";
import {SingleOpenWinService} from "../../shared/service/single-open-win.service";
import {VehicleTableComponent} from "../../shared/component/vehicle-table/vehicle-table.component";
import {CamApplyService} from "../../shared/service/cam-apply.service";
import {Application} from "../../shared/model/application";
import {Attach} from "../../shared/model/attach";
import MTSConstants from "../../shared/config/config";
import {Vehicle} from "../../shared/model/vehicle";

@Component({
  selector: 'app-cam-apply-by-manager',
  templateUrl: './cam-apply-by-manager.component.html',
  styleUrls: ['./cam-apply-by-manager.component.css']
})
export class CamApplyByManagerComponent implements OnInit {

  _checked:boolean;
  userId:string = this.routerInfo.snapshot.queryParams["userId"];
  psn:string = this.routerInfo.snapshot.queryParams["psn"];
 // psn = "PROCESSA00998";
  camForm:FormGroup;
  application:Application;
  purposes:Purpose[];
  fileList: Attach[] = [];
  @ViewChild(DriverTableComponent) driverTable:DriverTableComponent;
  @ViewChild(VehicleTableComponent) vehicleTable:VehicleTableComponent;

  _disabledButton = true;
  _disabledButtonEdit = true;

  //选择人员分页
  _current = 1;
  _pageSize = 10;
  _total = 1;
  _dataSet = [];

  constructor(
    private router:Router,
    private routerInfo:ActivatedRoute,
    private fb:FormBuilder,
    private singleOpenWinService:SingleOpenWinService,
    private camApplyService:CamApplyService
  ) { }

  ngOnInit() {
    this.createForm();

    this.getPurposeInfo();

    //订阅车辆开窗
    this.singleOpenWinService.vehicleChange.subscribe((info:string)=>{
      $('#car').val(this.vehicleTable.selected.vehicle.oid);
      $('#carName').val(this.vehicleTable.selected.vehicle.name);
      $('#licenseNumber').val(this.vehicleTable.selected.vehicle.licenseNumber);
      if(this.vehicleTable.selected.vehicle.passengerCapacity){
        $('#passengerCapacity').val(this.vehicleTable.selected.vehicle.passengerCapacity);
      }else{
        $('#passengerCapacity').val("");
      }
      if(this.vehicleTable.selected.vehicletype){
        $('#vehicleType').val(this.vehicleTable.selected.vehicletype.oid);
        $('#vehicleTypeName').val(this.vehicleTable.selected.vehicletype.name);
      }else{
        $('#vehicleType').val("");
        $('#vehicleTypeName').val("");
      }
      if(this.vehicleTable.selected.driver){
        $('#driver').val(this.vehicleTable.selected.driver.oid);
        $('#driverName').val(this.vehicleTable.selected.driver.name);
        $('#driverPhone').val(this.vehicleTable.selected.driver.phone);
      }else{
        $('#driver').val("");
        $('#driverName').val("");
        $('#driverPhone').val("");
      }

      this.vehicleIsVisible = false;
    });

    //订阅司机开窗
    this.singleOpenWinService.driverChange.subscribe((info:string)=>{
      $('#driver').val(this.driverTable.selected.oid);
      $('#driverName').val(this.driverTable.selected.name);
      $('#driverPhone').val(this.driverTable.selected.phone);
      this.driverIsVisible = false;
    });

    this.camApplyService.getApplicationByPsn(this.psn).then(res=>{
      this.application = res.application;
      this.camForm.patchValue({
        oid: this.application.oid,
        subject: this.application.subject,
        purpose: this.application.purpose,
        passengerNum: this.application.passengerNum,
        starttime: this.application.starttime,
        endtime: this.application.endtime,
        applicant:this.application.applicant,
        contact: this.application.contact,
        phone: this.application.phone,
        startpoint: this.application.startpoint,
        endpoint: this.application.endpoint,
        shared: this.application.shared,
        reason: this.application.reason,
        passengers: this.application.passengers,
        res01: this.application.res01,
        description: this.application.description
      });

      if(this.application.shared === 1){
        this._checked = true;
      }else{
        this._checked = false;
      }

      this.fileList = res.attachments;
      for(var i=0;i<this.fileList.length;i++){
        this.fileList[i].filePath = `${MTSConstants.HOSTUser}attach/download/${this.fileList[i].oid}`;
      }

      for(var i=0;i<this.purposes.length;i++){
        if(this.application.purpose == this.purposes[i].oid){
          $("#purpose").val(this.purposes[i].name);
          break;
        }
      }
    });
  }

  createForm(){
    this.camForm =this.fb.group({
      oid:'',
      subject:'',
      purpose:'',
      passengerNum:'',
      starttime:'',
      endtime:'',
      applicant:this.userId,
      contact:'',
      phone:'',
      startpoint:'',
      endpoint:'',
      shared:'',
      reason:'',
      passengers:'',
      res01:'',
      description:'',
      car:'',
      carName:'',
      vehicleType:'',
      vehicleTypeName:'',
      licenseNumber:'',
      passengerCapacity:'',
      driver:'',
      driverName:'',
      driverPhone:''
    });
  }

  //初始化用车性质
  getPurposeInfo(){
    this.camApplyService.getPurposes().then(res=>{
      this.purposes = res;
    });
  }

  //提交
  onSubmit(val:any){
    if(this.checkData()){

    }
  }

  /**
   * 单选司机开窗
   * @type {boolean}
   */
  driverIsVisible = false;

  driverShowModal = () => {
    this.driverIsVisible = true;
  }

  driverHandleCancel = (e) => {
    this.driverIsVisible = false;
  }

  //选择司机
  btn_select_driver(){
    this.driverTable.ngOnInit();
    this.driverShowModal();
  }

  /**
   * 单选车辆开窗
   * @type {boolean}
   */
  vehicleIsVisible = false;

  vehicleShowModal = () => {
    this.vehicleIsVisible = true;
  }

  vehicleHandleCancel = (e) => {
    this.vehicleIsVisible = false;
  }

  //选择司机
  btn_select_vehicle(){
    this.vehicleTable.ngOnInit();
    this.vehicleShowModal();
  }

  //新增修改表格数据卡控
  checkData(){
    let msg ="";
    if(!$("#carrName").val()){
      msg +=("请选择车辆信息！\n");
    }
    if(!$("#driverName").val()){
      msg +=("请选择司机信息！\n");
    }
    if(msg){
      alert(msg);
      return false;
    }else{
      return true;
    }

  }
}
